package com.appkubes.portal.admin.application.app.admin.controller;

import com.appkubes.common.Response;
import com.appkubes.portal.admin.application.app.admin.entity.vo.AdminAddVO;
import com.appkubes.portal.admin.application.app.admin.entity.vo.AdminGetVO;
import com.appkubes.portal.admin.application.app.admin.service.AppAdminService;
import com.appkubes.portal.admin.application.app.menu.entity.vo.MenuAdminGetVO;
import com.appkubes.portal.admin.application.app.menu.service.AppMenuService;
import com.appkubes.portal.admin.application.auth.service.KeyCloakUserService;
import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.keycloak.representations.idm.UserRepresentation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@Api(tags = "管理员模块")
@RestController
@RequestMapping("/app/admin")
public class AppAdminController {

    @Resource
    AppMenuService appMenuService; //菜单服务
    @Resource
    KeyCloakUserService keyCloakUserService;
    @Resource
    AppAdminService appAdminService;

    @ApiOperation("获取管理员列表")
    @GetMapping("/get")
    public Response get(@ParamValid AdminGetVO adminGetVO) {
        IPage<?> pageResult = keyCloakUserService.getPages(adminGetVO.getPage(), adminGetVO.getSize());//this.selectPageByEntity(adminGetVO);
        return Response.success(pageResult);
    }

    @ApiOperation("编辑管理员")
    @PostMapping("/save")
    public Response<UserRepresentation> save(@ParamValid AdminAddVO adminAddVO) {
        UserRepresentation result = appAdminService.save(adminAddVO);
        return Response.success(result);
    }

    @ApiOperation("获取管理员信息")
    @GetMapping("/getInfo/{userName}")
    public Response get(@PathVariable("userName") String userName) {
        //在SSO中有注册,则进行补偿注册
        UserRepresentation userRepresentation = keyCloakUserService.getUserRepresentation(userName);
        return Response.success(userRepresentation);
    }

    @ApiOperation("通过ID获取信息")
    @GetMapping("/getById/{userId}")
    public Response getById(@PathVariable("userId") String userId) {
        UserRepresentation userRepresentation = keyCloakUserService.getUserRepresentationById(userId);
        return Response.success(userRepresentation);
    }


    @ApiOperation("通过ID获取信息")
    @GetMapping("/delete/{userId}")
    public Response delete(@PathVariable("userId") String userId) {
        keyCloakUserService.deleteById(userId);
        return Response.success();
    }


    @ApiOperation("获取用户有权限的菜单")
    @GetMapping("/getMenuList")
    public Response getMenuByUser(@ParamValid MenuAdminGetVO menuAdminGetVO) {
        Map<?, ?> menuMap = appMenuService.getAdminMenu(menuAdminGetVO);
        return Response.success(menuMap);
    }
}
